/*
 * @Description: 新建
 * @Author: Zzt
 * @Date: 2022-12-21 17:02:19
 * @LastEditTime: 2023-01-03 17:00:45
 */
import { Button, Form, Modal } from 'antd';
import { useEffect } from 'react';
import { useBusinessGuidanceContext } from '../../contexts/BusinessGuidanceContext';
import { useGuidanceTypeContext } from '../../contexts/GuidanceWaysContext';
import FormFeedback from './FormFeedback';
import FromList from './FromList';
import TabelList from './TabelList';

const CreateBusinessDialog = (props: any) => {
  const { type, title } = useGuidanceTypeContext();
  const { selectUnit, addGuidanceList, addAnnualList, enclosureList, ClearAnnex } =
    useBusinessGuidanceContext();

  const { visible, onClose } = props;
  const [form] = Form.useForm();

  useEffect(() => {
    ClearAnnex();
  }, [visible]);
  const onCancel = () => {
    onClose();
  };

  /**
   * @description 提交表单，触发onFinish方法
   */
  const submitFunc = () => {
    form.submit();
  };

  // 新增方法
  const onFinish = (values: any) => {
    console.log('values', values);
    type === 1 &&
      addGuidanceList({
        ...values,
        type,
        instructedUnitId: selectUnit.id,
        attachmentJsonDTOS: enclosureList
      });
    type === 2 &&
      addGuidanceList({
        ...values,
        type,
        attachmentJsonDTOS: enclosureList
      });
    type === 3 &&
      addAnnualList({
        ...values,
        instructedUnitId: selectUnit.id
      });

    onClose();
  };

  return (
    <Modal
      title={`${title}-新增`}
      visible={visible}
      width={600}
      destroyOnClose
      onCancel={onCancel}
      footer={[
        <Button key="close" onClick={onCancel}>
          取消
        </Button>,
        <Button
          key="save"
          type="primary"
          onClick={() => {
            submitFunc();
          }}
        >
          创建
        </Button>
      ]}
    >
      <Form
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 19 }}
        preserve={false}
        form={form}
        onFinish={onFinish}
        labelAlign={'left'}
      >
        {type === 1 && <FromList selectUnit={selectUnit} />}
        {type === 2 && <FormFeedback />}
        <TabelList />
      </Form>
    </Modal>
  );
};
export default CreateBusinessDialog;
